package nccloud.web.gl.voucher.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

import com.alibaba.fastjson.JSON;

import nc.bs.voucher.itf.IVoucherDetailPrintService;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFDouble;
import nc.vo.trade.voutils.SafeCompute;
import nccloud.dto.gl.detailass.RequestDetialAssDTO;
import nccloud.framework.core.exception.ExceptionUtils;
import nccloud.framework.core.io.WebFile;
import nccloud.framework.service.ServiceLocator;
import nccloud.framework.web.action.itf.ICommonAction;
import nccloud.framework.web.container.IRequest;
import nccloud.nc.ui.trade.excelimport.io.XLSFileUtil;
import nccloud.pubitf.gl.lightgl.voucher.IVoucherWebExtHrpService;
import nccloud.ui.trade.excelimport.parser.IFileParserConstants;
import nccloud.web.codeplatform.framework.action.base.VOTransform;

public class exportDataAction implements ICommonAction {

	@Override
	public Object doAction(IRequest request) {
		String busiParamJson = null;
		Map<String, String[]> parameters = request.readParameters();
		if (!parameters.isEmpty() && parameters.get("busiParamJson") != null) {
			busiParamJson = JSON.parseObject(parameters.get("busiParamJson")[0]).getString("param");
		}
		RequestDetialAssDTO param = VOTransform.toVO(busiParamJson, RequestDetialAssDTO.class);
		IVoucherWebExtHrpService extHrpService = ServiceLocator
				.find(IVoucherWebExtHrpService.class);
		try {
			Map<String, Object> map = extHrpService.queryLinkAssColumnAndValue(param);
			if(map != null) {
				List<Map<String, Object>> listcolumns =  (ArrayList<Map<String, Object>>)map.get("columns");
				List<Map<String, Object>> listrows =  (ArrayList<Map<String, Object>>)map.get("rows");
				UFDouble jfje = new UFDouble(0);
				UFDouble dfje = new UFDouble(0);
				HashMap<Object, Object> mappk = new HashMap<Object,Object>();
				ArrayList<Object> listcols = new ArrayList<Object>();
				ArrayList<ArrayList<Object>> listvalues = new ArrayList<ArrayList<Object>>();
				if(listcolumns != null && !listcolumns.isEmpty()) {
					listcols.add("分录号");
					listcols.add("摘要");
					for(int i = 0 ; i < listcolumns.size(); i++) {
						Map<String, Object> mapcol = listcolumns.get(i);
						Object value = mapcol.get("checktypename");
						mappk.put(i, mapcol.get("pk_accassitem"));
						listcols.add(value);
					}
					listcols.add("借方金额");
					listcols.add("贷方金额");
					listvalues.add(listcols);
				}
				if(listrows != null && !listrows.isEmpty()) {
					for(int i = 0 ; i < listrows.size(); i++) {
						ArrayList<Object> item = new ArrayList<Object>();
						Map<String, Object> mapcol = listrows.get(i);
						Map<String, Object> mapindes =  (Map<String, Object>)mapcol.get("detailindex");
						Map<String, Object> explanation =  (Map<String, Object>)mapcol.get("explanation");
						item.add(mapindes.get("value"));
						item.add(explanation.get("value"));
						for(int j =0 ; j< mappk.size(); j++) {
							Object asscol_ = "asscol_"+mappk.get(j);
							Map<String, Object> m = (Map<String, Object>)mapcol.get(asscol_);
							item.add(m.get("display"));
						}
						Map<String, Object> debitamount =  (Map<String, Object>)mapcol.get("debitamount");
						if(debitamount != null) {
							item.add(debitamount.get("value"));
							item.add("");
							dfje = SafeCompute.add(dfje, new UFDouble(debitamount.get("value").toString()));
						}else {
							Map<String, Object> creditamount =  (Map<String, Object>)mapcol.get("creditamount");
							item.add("");
							item.add(creditamount.get("value"));
							jfje = SafeCompute.add(jfje, new UFDouble(creditamount.get("value").toString()));
						}
						listvalues.add(item);
					}
				}
				ArrayList<Object> hj = new ArrayList<Object>();
				hj.add("合计");
				hj.add("");
				for(Object k : mappk.keySet()) {
					hj.add("");
				}
				hj.add(dfje.setScale(2, UFDouble.ROUND_HALF_UP));
				hj.add(jfje.setScale(2, UFDouble.ROUND_HALF_UP));
				listvalues.add(hj);//
				IVoucherDetailPrintService service = ServiceLocator.find(IVoucherDetailPrintService.class);
				InputStream inputStream = service.VoucherDetailPrint(listvalues);
				return new WebFile("辅助明细" + ".xlsx", inputStream);
			}
		} catch (Exception e) {
			ExceptionUtils.wrapException(e);
			return null;
		}
		return true;
	}

}
